package com.xbongbong.workorder.explain;

import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.interfaces.explain.BusinessExplainHandle;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.saas.domain.entity.WorkOrderFormEntity;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperatePageEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.WorkOrderEnum;
import com.xbongbong.saas.help.ExplainHelp;
import com.xbongbong.saas.model.WorkOrderFormModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/01/19 17:12
 */
@Component
public class WorkOrderExplainHandleImpl implements BusinessExplainHandle,WorkOrderExplainHandle {

    @Resource
    private ExplainHelp explainHelp;
    @Resource
    private WorkOrderFormModel workOrderFormModel;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.WORK_ORDER.getCode());
    }

    @Override
    public List<FieldAttrEntity> newHandleExplain(FormatExplainDTO formatExplainDTO) throws XbbException {
        List<FieldAttrEntity> explainList = formatExplainDTO.getExplainList();
        String corpid = formatExplainDTO.getCorpid();
        for (FieldAttrEntity fieldAttrEntity : explainList) {
            String attr = fieldAttrEntity.getAttr();
            if (Objects.equals(attr, WorkOrderEnum.SPARE_PARTS.getAttr())) {
                explainHelp.formatSelectProductExplains(fieldAttrEntity, XbbRefTypeEnum.WORK_ORDER, corpid, OperatePageEnum.NEW.getCode(), false);
                fieldAttrEntity.setSupportScanning(1);
            }
        }
        return explainList;
    }

    @Override
    public void setTemplate(List<WorkOrderFormEntity> workOrderFormList, Long formId, List<FieldAttrEntity> explainList) throws XbbException {

        FieldAttrEntity templateFieldAttrEntity = new FieldAttrEntity();
        if (workOrderFormList != null && workOrderFormList.size() == 1) {
            templateFieldAttrEntity.setVisible(0);
        } else {
            templateFieldAttrEntity.setVisible(1);
        }
        templateFieldAttrEntity.setAttrName(OperateModuleTypeEnum.WORK_ORDER.getName());
        templateFieldAttrEntity.setAttr(FieldTypeEnum.TEMPLATE.getAlias());
        templateFieldAttrEntity.setAttrType(FieldTypeEnum.TEMPLATE.getEsalias());
        templateFieldAttrEntity.setFieldType(FieldTypeEnum.TEMPLATE.getType());
        templateFieldAttrEntity.setEditable(1);
        templateFieldAttrEntity.setComboType(0);
        templateFieldAttrEntity.setDefaultAttr(new DefaultAttrPoJo());
        List<ItemPoJo> templateItem = new ArrayList<>();
        for (WorkOrderFormEntity entity : workOrderFormList) {
            ItemPoJo itemPoJo = new ItemPoJo();
            itemPoJo.setValue(entity.getId());
            itemPoJo.setText(entity.getName());
            if (Objects.equals(entity.getId(), formId)) {
                itemPoJo.setChecked(true);
            }
            templateItem.add(itemPoJo);
        }
        templateFieldAttrEntity.setItems(templateItem);
        explainList.add(0, templateFieldAttrEntity);

    }

    @Override
    public void setTemplate(List<FieldAttrEntity> list, UserVO userVO, WorkOrderFormEntity workOrderFormEntity) {
        /*
         * 工单模板选择，模拟解释的方式实现，不刷在解释里，编辑回显，我不想加的，凤娇一定要
         * start
         * */
        List<WorkOrderFormEntity> workOrderFormList = workOrderFormModel.getTemplateWithPermission(userVO.getCorpid(), userVO, workOrderFormEntity.getAppId(), null);
        FieldAttrEntity templateFieldAttrEntity = new FieldAttrEntity();
        if (workOrderFormList != null && workOrderFormList.size() == 1) {
            templateFieldAttrEntity.setVisible(0);
        } else {
            templateFieldAttrEntity.setVisible(1);
        }
        templateFieldAttrEntity.setAttrName(OperateModuleTypeEnum.WORK_ORDER.getName());
        templateFieldAttrEntity.setAttrType(FieldTypeEnum.TEMPLATE.getEsalias());
        templateFieldAttrEntity.setAttr(FieldTypeEnum.TEMPLATE.getAlias());
        templateFieldAttrEntity.setFieldType(FieldTypeEnum.TEMPLATE.getType());
        templateFieldAttrEntity.setEditable(0);
        templateFieldAttrEntity.setComboType(0);
        List<ItemPoJo> templateItem = new ArrayList<>();
        ItemPoJo itemPoJo = new ItemPoJo();
        itemPoJo.setValue(workOrderFormEntity.getId());
        itemPoJo.setText(workOrderFormEntity.getName());
        itemPoJo.setChecked(true);
        templateItem.add(itemPoJo);
        templateFieldAttrEntity.setItems(templateItem);
        list.add(0, templateFieldAttrEntity);
    }
}
